<?php

namespace app\api\model;

use app\common\enums\StatusEnum;
use think\Db;
use app\common\controller\Send;

class Oauth extends \app\common\model\Oauth
{
    /**
     * 添加用户数据
     * @param $openid
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function addOne($openid)
    {
        $oauth = new Oauth();
        $oauth->openid = $openid;
        $oauth->share_key = self::shareKey();
        $oauth->status = StatusEnum::ENABLED;
        $oauth->save();
        return $oauth->id;
    }

    /**
     * 添加用户数据
     * @param $openid
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function addOne2($openid,$unionid)
    {
        $oauth = new Oauth();
        $oauth->openid = $openid;
        $oauth->share_key = self::shareKey();
        $oauth->status = StatusEnum::ENABLED;
        $oauth->unionid = $unionid;
        $oauth->save();
        return $oauth->id;
    }

    /**
     * 根据openid更新一条数据
     * @param $openid
     * @param $data
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function updateOne($openid, $data)
    {
        $user = self::getByOpenId($openid);
        if (empty($user)) {
            $user = new Oauth;
            $user->openid = $openid;
            $user->share_key = self::shareKey();
            $user->status = StatusEnum::ENABLED;
            $user->autoplay = self::AUTOPLAY;
        }
        //$user->unionid = isset($data['unionid']) ? $data['unionid'] : '';
        $user->nickname = $data['nickname'];
        $user->sex = $data['sex'];
        $user->province = $data['province'];
        $user->city = $data['city'];
        $user->country = $data['country'];
        $user->head_img = $data['head_img'];
        $user->unionid = $data['unionid'];
        return $user->save() ? $user : [];
    }

    /**
     * 根据unionid修改
     */
    public static function updateOpenid($unionid,$openid){
        $user = self::getByUnionid($unionid);
        $user->openid = $openid;
        return $user->save() ? $user : [];
    }

    /**
     * 根据openid更新手机号码
     * @param $openid
     * @param $data
     * @return boolean
     * @throws \think\Exception\DbException
     */
    public static function updatePhone($openid, $data)
    {
        $user = self::getByOpenId($openid);
        if ($user) {
            $user->phone = $data['phone'];
            $user->pure_phone = $data['pure_phone'];
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * 编辑个性签名
     * @param $oauthId
     * @param $content
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function signature($oauthId, $content)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->signature = $content;
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * 编辑是否自动播放
     * @param $oauthId
     * @param $autoplay
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function autoplay($oauthId, $autoplay)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->autoplay = $autoplay;
            return $user->save() ? true : false;
        }
        return false;
    }

    /**
     * id随机key
     * @param int $length 长度
     * @return string
     * @throws \think\Exception\DbException
     */
    private static function shareKey($length = 10)
    {
        $key = rand_char($length);
        $next = true;
        while ($next) {
            $model = Oauth::where(['share_key' => $key])->find();
            if (empty($model)) {
                $next = false;
            } else {
                $key = rand_char($length);
            }
        }
        return $key;
    }


    public static function updateStatus($oauthId, $status)
    {
        $user = self::getOne($oauthId);
        if ($user) {
            $user->status = $status;
            return $user->save() ? true : false;
        }
        return false;
    }
        /**
         * 查找分享码
         * @param $code
         */
        public static function getCode ($code) {
            return  static::where('code',$code)->find();
    }
    /**
     * 指纹列表
     */
    public static  function getFingerprint($oauthId)
    {

        $list = Db::name('oauth_fingerprint')->where('oauth_id',$oauthId)->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $num = $k;
                $num++;
                $list[$k]['name'] = '指纹'.$num;
            }
        }
        dump($list);
        return $list ? true : false;
    }
    /**
     * 添加指纹
     */
    public static function addFingerprint($oauthId,$fingerprint) {
        $data = [];
        $data = [
        'oauth_id'=>$oauthId,
        'fingerprint'=>$fingerprint,
        'type'=>1,
        'create_time'=>time(),
        ];
        $info = Db::name('oauth_fingerprint')->insert($data);
        return $info ? true : false;
    }
    /**
     * 删除指纹
     */
    public static function delFingerprint($oauthId,$fingerprint) {
        $where = [];
        $where['oauth_id'] = $oauthId;
        $where['fingerprint'] = $fingerprint;
        $info = Db::name('oauth_fingerprint')->where($where)->delete();
        return $info ? true : false;
    }
}